package pt.inescid.components.policyengine.heimdall.commons;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import pt.inescid.components.policyengine.heimdall.base.Config;
import pt.inescid.components.policyengine.heimdall.database.ConfigDAO;

public class ProjectInfo {

	public static final String PROJECT_DB_USER = "heimdall_user";

	// Heimadall features (stored in Config)
	public static final int CACHE_FEATURE = 0;
	public static final int REMOTE_FEATURE = 1;

	public static final String[] PROJECT_FEATURE_TAGS = {
		"cache", "remote"
	};
	
	public static final String APP_NAME_PROPERTY = "appName";
	public static final String WEBAPP_PROPERTY = "webapp";
	public static final String HOST_NAME_PROPERTY = "localhost";
	public static final String HOST_PORT_PROPERTY = "3306";
	public static final String DB_NAME_PROPERTY = "dbName";
	public static final String DB_USER_PROPERTY = "root";
	public static final String PROJECT_VERSION_PROPERTY = "version";

	private static Properties properties = null;

	
	/* ===========================================================
	==============================================================
	Get Property
	==============================================================
	=========================================================== */
	public static String getProperty(String field) {
		if (properties == null) {
			try {
				InputStream in = new ProjectInfo().getClass().
						getClassLoader().getResourceAsStream(
						"heimdall.properties");
				properties = new Properties();
				properties.load(in);
			} catch (IOException e) {
				System.out.println("Properties not available: " + e);
			}		
		}
		
		return (properties.getProperty(field));
	} // getProperty
	
	
	
//	/* ===========================================================
//	==============================================================
//	setProperty
//	==============================================================
//	=========================================================== */
//	public static void setProperty(String field, String value) {
//		
//		if (properties == null) {
//			properties = new Properties();
//		}
//		
//		properties.put(field, value);
//	} // setProperty
	
	
	public static boolean isEnabled(int feature) {
		Config record = ConfigDAO.getRecordByTag(
				PROJECT_FEATURE_TAGS[feature]);
		String value = null;
		
		if (record != null) {
			value = record.getValue();
		}
		else {
			return (false);
		}
		
		return (value.equals("enabled"));
	}

}; // ProjectInfo
